package com.lzh.others.java8.book.chapter_3_3常用流操作;

import java.util.IntSummaryStatistics;
import java.util.List;
import java.util.function.BiFunction;
import java.util.function.BinaryOperator;
import java.util.function.IntBinaryOperator;
import java.util.stream.Collectors;
import java.util.stream.Stream;


public class reduceTest {

    public static void main(String[] args) {

        Integer reduce = Stream.of(1, 2, 3).reduce(0, (acc, element) -> acc + element);

        BiFunction<Integer,Integer,Integer> accumulator3 = (acc, element) -> acc + element;
        int count3 = accumulator3.apply(
                accumulator3.apply(
                        accumulator3.apply(0, 1),
                        2),
                3);

        BinaryOperator<Integer> accumulator = (acc, element) -> acc + element;
        int count = accumulator.apply(
                accumulator.apply(
                        accumulator.apply(0, 1),
                        2),
                3);
        IntBinaryOperator accumulator2 = (acc, element) -> acc + element;
        int count2 = accumulator2.applyAsInt(
                accumulator2.applyAsInt(
                        accumulator2.applyAsInt(0, 1),
                        2),
                3);
        System.out.println("reduce = " + reduce);
        System.out.println("BinaryOperatori = " + count);
        System.out.println("IntBinaryOperator = " + count2);
        System.out.println("BiFunction = " + count3);


        IntSummaryStatistics intSummaryStatistics = Stream.of(Stream.of(1, 3), Stream.of(0, 2), Stream.of(1, 2)).mapToInt(
                total -> {
                    List<Integer> collect = total.collect(Collectors.toList());
                    return collect.size();
                }
        ).summaryStatistics();
        System.out.printf("Max: %d, Min: %d, Ave: %f, Sum: %d ",
                intSummaryStatistics.getMax(),
                intSummaryStatistics.getMin(),
                intSummaryStatistics.getAverage(),
                intSummaryStatistics.getSum());

    }
}
